﻿using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace ZhukBlogSharePointProject
{
    public static class Extensions
    {
        public static PickerEntity ToPickerEntity(this SPFieldLookupValue lookupValue)
        {
            return new PickerEntity
            {
                DisplayText = lookupValue.LookupValue,
                Key = lookupValue.LookupId.ToString()
            };
        }

        public static void SetAttribute(this SPField field, string attribute, string value)
        {
            var baseType = field.GetType().BaseType;
            var mi = baseType.GetMethod("SetFieldAttributeValue", BindingFlags.Instance | BindingFlags.NonPublic);
            mi.Invoke(field, new object[] { attribute, value });
        }

        public static string GetAttribute(this SPField field, string attribute, string value)
        {
            var baseType = field.GetType().BaseType;
            var mi = baseType.GetMethod("GetFieldAttributeValue", BindingFlags.Instance | BindingFlags.NonPublic, null, new [] { typeof(string) }, null);
            var obj = mi.Invoke(field, new object[] { attribute });
            return obj == null ? "" : obj.ToString();
        }
    }
}
